package com.ruoyi.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.domain.vo.TLaboratoryVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.admin.domain.TLaboratory;
import com.ruoyi.admin.service.ITLaboratoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验室存储管理Controller
 * 
 * @author ruoyi
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/lab/laboratory")
public class TLaboratoryController extends BaseController
{
    @Autowired
    private ITLaboratoryService tLaboratoryService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询实验室存储管理列表
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLaboratory tLaboratory)
    {
        startPage();
        List<TLaboratoryVo> list = tLaboratoryService.selectTLaboratoryList(tLaboratory);
        return getDataTable(list);
    }

    /**
     * 导出实验室存储管理列表
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:export')")
    @Log(title = "实验室存储管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TLaboratory tLaboratory)
    {
        List<TLaboratoryVo> list = tLaboratoryService.selectTLaboratoryList(tLaboratory);
        ExcelUtil<TLaboratoryVo> util = new ExcelUtil(TLaboratoryVo.class);
        util.exportExcel(response, list, "实验室存储管理数据");
    }

    /**
     * 获取实验室存储管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:query')")
    @GetMapping(value = "/{labId}")
    public AjaxResult getInfo(@PathVariable("labId") Long labId)
    {
        return success(tLaboratoryService.selectTLaboratoryByLabId(labId));
    }

    /**
     * 新增实验室存储管理
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:add')")
    @Log(title = "实验室存储管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLaboratory tLaboratory)
    {
        SysUser sysUser = sysUserService.selectUserById(tLaboratory.getLabAssistantId());
        if (sysUser == null){
            return error("新增实验室存储信息失败");
        }
        return toAjax(tLaboratoryService.insertTLaboratory(tLaboratory));
    }

    /**
     * 修改实验室存储管理
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:edit')")
    @Log(title = "实验室存储管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLaboratory tLaboratory)
    {
        SysUser sysUser = sysUserService.selectUserById(tLaboratory.getLabAssistantId());
        if (sysUser == null){
            return error("新增实验室存储信息失败");
        }
        return toAjax(tLaboratoryService.updateTLaboratory(tLaboratory));
    }

    /**
     * 删除实验室存储管理
     */
    @PreAuthorize("@ss.hasPermi('lab:laboratory:remove')")
    @Log(title = "实验室存储管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{labIds}")
    public AjaxResult remove(@PathVariable Long[] labIds)
    {
        return toAjax(tLaboratoryService.deleteTLaboratoryByLabIds(labIds));
    }
}
